package com.zhuozhou.jdyc.main.base.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;

import androidx.core.content.ContextCompat;

import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.api.AppManager;
import com.zhuozhou.jdyc.api.AppSettingHelper;
import com.zhuozhou.jdyc.api.ConstantsHelper;
import com.zhuozhou.jdyc.base.BaseActivity;
import com.zhuozhou.jdyc.base.BaseViewModel;
import com.zhuozhou.jdyc.databinding.LockviewBinding;
import com.zhuozhou.jdyc.util.apiUtil.StringUtil;
import com.zhuozhou.jdyc.util.jiami.MD5Util;
import com.zhuozhou.jdyc.wight.lockView.LockView;

import java.util.List;


/**
 * Created by 方舟 on 2017/6/29.
 * 九宫格手势密码
 */
public class LockViewActivity extends BaseActivity<BaseViewModel, LockviewBinding> implements LockView.OnDrawFinishedListener {
    private String lockStr;
    private boolean isSet = false;

    @Override
    protected int getLayoutId() {
        return R.layout.lockview;
    }

    @Override
    protected boolean hasToolBar() {
        return false;
    }

    @Override
    public String setTitleBar() {
        return "手势密码";
    }

    @Override
    public void initData(Bundle bundle) {
        isSet = bundle.getBoolean("isSet", false);
    }

    public static void show(Activity context) {
        Intent intent = new Intent(context, LockViewActivity.class);
        context.startActivityForResult(intent, ConstantsHelper.LOOK_VIEW_ACTIVITY_REQUEST_CODE);
    }

    public static void show(Activity context, Bundle bundle) {
        Intent intent = new Intent(context, LockViewActivity.class);
        intent.putExtras(bundle);
        context.startActivityForResult(intent, ConstantsHelper.LOOK_VIEW_ACTIVITY_REQUEST_CODE);
    }

    //初始化布局
    @Override
    public void initView() {
        if (StringUtil.isEmpty(AppSettingHelper.getGestures())) {
            binding.tips.setText("您还未设置手势密码，请绘制手势密码");
        } else {
            binding.tips.setText("请绘制手势密码");
        }
        binding.lockView.setOnDrawFinishedListener(this);
    }

    //当绘制完成后调用此方法，用来判断成功还是失败
    @Override
    public boolean onDrawFinished(List<Integer> passPositions) {
        StringBuilder sb = new StringBuilder();
        for (Integer i : passPositions) {
            sb.append(i.intValue());
        }
        // 把字符串md5加密后存入本地
        String md5Str = "";
        try {
            md5Str = MD5Util.md5Encode(sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 比较路径是否一致
//                boolean valid = comparePath(sb.toString());
        boolean valid = false;

        if (StringUtil.isEmpty(AppSettingHelper.getGestures())) {
            if (isShort(sb.toString())) {
                return false;
            }
            if (lockStr == null) {
                lockStr = md5Str;
                valid = true;
                setTips("请再次绘制密码", R.color.autoColor);
            } else {
                if (lockStr.equals(md5Str)) {
                    AppSettingHelper.setGestures(md5Str);
                    AppSettingHelper.setGesturesIsOpen(true);
                    valid = true;
                    setTips("手势密码设置成功", R.color.autoColor);
                    setResultBack();
                } else {
                    setTips("手势密码错误，请重新绘制", R.color.theme_red);
                    lockStr = null;
                    valid = false;
                }
            }
        } else {
            if (!StringUtil.isEmpty(AppSettingHelper.getGestures()) && AppSettingHelper.getGestures().equals(md5Str)) {
                setResultBack();
                valid = true;
            } else {
                valid = false;
                setTips("手势密码错误，请重新绘制", R.color.theme_red);
            }
        }
        binding.lockView.setClickable(false);
        new Handler().postDelayed(() -> {
            binding.lockView.resetPoints();
            binding.lockView.setClickable(true);
        }, 300);
        return valid;
    }

    private void setTips(String tipsMsg, int color) {
        binding.tips.setText(tipsMsg);
        binding.tips.setTextColor(ContextCompat.getColor(this, color));
    }

    private boolean isShort(String lock) {
        if (lock.length() < 4) {
            setTips("最少链接4个点，请重新设置", R.color.theme_red);
            binding.lockView.resetPoints();
            return true;
        }
        return false;
    }

    //设置返回结果，在启动此activity的OnActivityResult中接受结果
    private void setResultBack() {
        setResult(RESULT_OK);
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (AppSettingHelper.getGesturesIsOpen() && !isSet) {
                AppManager.getAppManager().AppExit(this);
            }
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

}
